home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / pascal / tptc17sc.zip / TPTCMAC.H < prev    next >
C/C++ Source or Header  |  1988-03-26  |  6KB  |  258 lines

  1.  
  2. /*
  3.  * TPTCMAC.H - Macro Header for use with Turbo Pascal --> C Translator
  4.  *
  5.  * (C) 1986 S.H.Smith (rev. 24-Mar-88)
  6.  *
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <stdarg.h>
  13. #include <dos.h>
  14. #include <conio.h>
  15. #include <ctype.h>
  16.  
  17.  
  18. /* define some simple keyword replacements */
  19.  
  20.  
  21. #define pred(v)         ((v)-1)
  22. #define succ(v)         ((v)+1)
  23. #define chr(n)          (n)
  24. #define ord(c)          (c)
  25. #define lo(v)           (v & 0xff)
  26. #define hi(v)           (v >> 8)
  27. #define inc(v)          ++(v)
  28. #define dec(v)          --(v)
  29.  
  30. #define maxint          0x7fff
  31. #define integer         int
  32. #define word            unsigned
  33. #define longint         long
  34. #define byte            char
  35. #define real            double
  36. #define boolean         int
  37. typedef void            *pointer;
  38.  
  39. #define false           0
  40. #define true            1
  41. #define nil             NULL
  42.  
  43.  
  44. #define delete(s,p,num) strcpy(s+p-1,s+p+num)
  45. #define val(s,res,code) code=0, res=atof(s)
  46.  
  47. typedef char *charptr;
  48. #define STRSIZ 255      /* default string length */
  49.  
  50. #define paramstr(n)     (argv[n])
  51. #define paramcount      (argc-1)
  52.  
  53.  
  54. /* 
  55.  * file access support 
  56.  */
  57.  
  58. char _CURNAME[64];
  59. int  ioresult = 0;
  60.  
  61. typedef FILE            *text;
  62. #define kbd             stdin
  63. #define input           stdin
  64. #define con             stdout
  65. #define output          stdout
  66.  
  67. #define assign(fd,name) strcpy(_CURNAME,name)
  68.  
  69. void    reset(text  *fd)
  70. {
  71.     *fd = fopen(_CURNAME,"r");
  72.     ioresult = (*fd == NULL);
  73. }
  74.  
  75. void    rewrite(text *fd)
  76. {
  77.     *fd = fopen(_CURNAME,"w");
  78.     ioresult = (*fd == NULL);
  79. }
  80.  
  81. void    append(text *fd)
  82. {
  83.     *fd = fopen(_CURNAME,"a");
  84.     ioresult = (*fd == NULL);
  85. }
  86.  
  87.     
  88. /*
  89.  *   setrec setof(a,b,...,-1)
  90.  *      construct and return a set of the specified character values
  91.  *
  92.  *   inset(ex,setrec)
  93.  *      predicate returns true if expression ex is a member of
  94.  *      the set parameter
  95.  *
  96.  */
  97. #define __  -2    /* thru .. */
  98. #define _E  -1    /* end of set marker */
  99.  
  100. typedef struct {
  101.       char setstub[16];
  102.    } setrec;
  103.  
  104.  
  105.  
  106. /*
  107.  * copy len bytes from the dynamic string dstr starting at position from
  108.  *
  109.  */
  110. charptr copy(charptr str,
  111.              int    from,
  112.              int    len)
  113. {
  114.    static char buf[STRSIZ];
  115.    buf[0]=0;
  116.    if (from>strlen(str))     /* copy past end gives null string */
  117.       return buf;
  118.  
  119.    strcpy(buf,str+from-1);  /* skip over first part of string */
  120.    buf[len] = 0;            /* truncate after len characters */
  121.    return buf;
  122. }
  123.  
  124.  
  125. /*
  126.  * String/character concatenation function
  127.  *
  128.  * This function takes a sprintf-like control string, a variable number of
  129.  * parameters, and returns a pointer a static location where the processed
  130.  * string is to be stored.
  131.  *
  132.  */
  133.  
  134. charptr scat(charptr control, ...)
  135. {
  136.    static char buf[STRSIZ];
  137.    char buf2[STRSIZ];
  138.    va_list args;
  139.  
  140.    va_start(args, control);     /* get variable arg pointer */
  141.    vsprintf(buf2,control,args); /* format into buf with variable args */
  142.    va_end(args);                /* finish the arglist */
  143.  
  144.    strcpy(buf,buf2);
  145.    return buf;                  /* return a pointer to the string */
  146. }
  147.  
  148.  
  149. #define ctos(ch) scat("%c",ch)  /* character to string conversion */
  150.  
  151.  
  152. /*
  153.  * string build - like scat, sprintf, but will not over-write any
  154.  *                input parameters
  155.  */
  156.  
  157. void sbld(charptr dest,
  158.           charptr control, ...)
  159. {
  160.    char buf[STRSIZ];
  161.    va_list args;
  162.  
  163.    va_start(args, control);     /* get variable arg pointer */
  164.    vsprintf(buf,control,args);  /* format into buf with variable args */
  165.    va_end(args);                /* finish the arglist */
  166.  
  167.    strcpy(dest,buf);            /* copy result */
  168. }
  169.  
  170.  
  171.  
  172. /*
  173.  * spos(str1,str2) - returns index of first occurence of str1 within str2;
  174.  *    1=first char of str2
  175.  *    0=nomatch
  176.  */
  177.  
  178. int spos(charptr str1,
  179.          charptr str2)
  180. {
  181.    charptr res;
  182.    res = strstr(str2,str1);
  183.    if (res == NULL)
  184.       return 0;
  185.    else
  186.       return res - str2 + 1;
  187. }
  188.  
  189.  
  190. /*
  191.  * cpos(str1,str2) - returns index of first occurence of c within str2;
  192.  *    1=first char of str2
  193.  *    0=nomatch
  194.  */
  195.  
  196. int cpos(char c,
  197.          charptr str2)
  198. {
  199.    charptr res;
  200.    res = strchr(str2,c);
  201.    if (res == NULL)
  202.       return 0;
  203.    else
  204.       return res - str2 + 1;
  205. }
  206.  
  207.  
  208.  
  209. /*
  210.  * Scanf/Fscanf support
  211.  *
  212.  * These functions operate like scanf and fscanf except for an added control
  213.  * code used for full-line reads.
  214.  *
  215.  */
  216.  
  217. int fscanv(text fd,
  218.            charptr control, ...)
  219. {
  220.    va_list args;
  221.    charptr arg1;
  222.    int     i;
  223.  
  224.    va_start(args, control);     /* get variable arg pointer */
  225.  
  226.    /* process special case for full-line reads (why doesn't scanf allow
  227.       full-line string reads?  why don't gets and fgets work the same?) */
  228.    if (*control == '#') {
  229.       arg1 = va_arg(args,charptr);
  230.       fgets(arg1,STRSIZ,fd);
  231.       arg1[strlen(arg1)-1] = 0;
  232.       return 1;
  233.    }
  234.  
  235.    /* pass the request on to fscanf */
  236.    i = vfscanf(fd,control,args);    /* scan with variable args */
  237.    va_end(args);                    /* finish the arglist */
  238.  
  239.    return i;                        /* return a pointer to the string */
  240. }
  241.  
  242. #undef atoi         /* in case of user ident clash */
  243. #undef getchar
  244.  
  245.  
  246. /*
  247.  * rename some tp4 calls that conflict with tc1.0 functions
  248.  *
  249.  */
  250.  
  251. #define intr    Pintr
  252. #define getdate Pgetdate
  253. #define gettime Pgettime
  254. #define setdate Psetdate
  255. #define settime Psettime
  256. #define keep    Pkeep
  257.  
  258.